package cn.tedu._07weibo.controller;

import cn.tedu._07weibo.base.response.JsonResult;
import cn.tedu._07weibo.base.response.StatusCode;
import cn.tedu._07weibo.mapper.CommentMapper;
import cn.tedu._07weibo.pojo.dto.CommentAddParam;
import cn.tedu._07weibo.pojo.entity.Comment;
import cn.tedu._07weibo.pojo.vo.CommentVO;
import cn.tedu._07weibo.pojo.vo.UserLoginVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name = "03-评论管理")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;


    @PostMapping("insert")
    @Operation(summary = "发布评论")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "weiboId"),
            @Parameter(name = "content"),
            @Parameter(name = "param",hidden = true)
    })
    public JsonResult insertComment(CommentAddParam param, HttpSession session){
        log.debug("发布信息："+param);
        UserLoginVO user=(UserLoginVO)session.getAttribute("user");
//        if (user==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(param,comment);
        comment.setCreateTime(new Date());
        comment.setUserId(user.getId());
        commentMapper.insertComment(comment);
        return JsonResult.OK();
    }

    @GetMapping("list")
    @Operation(summary = "查询微博列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listComment(Long weiboId,HttpSession session){
        log.debug("微博id："+weiboId);
        UserLoginVO user=(UserLoginVO)session.getAttribute("user");
//        if (user==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        List<CommentVO> list =commentMapper.listComment(weiboId);

        return JsonResult.OK(list);


    }
}
